var base_url;

$(document).ready(function() {
    base_url=$('#base_url').html();
    console.log("INFO: ************** PRINCIPAL CONTROLLER ****************");

    setCalendarEvent('#form1_fecha');
    setCalendarEvent('#form2_fecha');
    getTerminalesOrigenAjax('#form1');
    getTerminalesOrigenAjax('#form2');

    $('#form1_btn_consultar').click(function() {
        if (validateFindForm('#form1', '#mensajeControl1')) {
            if (validateUserLogged()) {
                callUrlFromJS('online/opciones');
            } else {
                saveParametersFromPrincipal('#form1');
                callUrlFromJSNewTab('website/login', '_blank');
            }
        }
    });

    $('#form2_btn_buscar').click(function() {
        if (validateFindForm('#form2', '#mensajeControl2')) {
            saveParametersFromPrincipal('#form2');
            callUrlFromJS('website/busqueda');
        }
    });

    $('#form1_origen').change(function() {
        if (validateSelect('#form1_origen option:selected')) {
            getTerminalesDestinoAjax('#form1');
        } else { //si es seleccionar
            $('#form1_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
        }
    });

    $('#form2_origen').change(function() {
        if (validateSelect('#form2_origen option:selected')) {
            getTerminalesDestinoAjax('#form2');
        } else { //si es seleccionar
            $('#form2_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
        }
    });

});

function saveParametersFromPrincipal(form){
    localStorage.fecha= $(form+'_fecha').val();
    localStorage.idTerminalOrigen = $(form+'_origen option:selected').val();
    localStorage.idTerminalDestino = $(form+'_destino option:selected').val();
    localStorage.origen = $(form+'_origen option:selected').text();
    localStorage.destino = $(form+'_destino option:selected').text();

    console.log("INFO: Save data from Principal Page");
}

function getTerminalesOrigenAjax(form){
    var method = WS_SERVER+'terminal/origenes/format/json';
    console.log("WS: "+method);

    $.ajax({
        type: 'get',
        url: method,
        dataType: 'json',
        data: '',
        cache: false,
        success: function(data){
            var cad= crearDropDownSelectTerminal(data);
            $(form+'_origen').html(cad);
            $(form+'_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log("ERROR: Ajax Request: "+textStatus+", "+errorThrown);
            console.log("ERROR_METHOD: getTerminalesOrigenAjax");
            console.log("ERROR_WS: "+method);
        }
    });
}

function getTerminalesDestinoAjax(form){
    deshabilitarElemento(form+'_destino');
    
    var idTerminalOrigen = $(form+'_origen option:selected').val();
    var method = WS_SERVER+'terminal/destinos/idTerminalOrigen/'+idTerminalOrigen+'/format/json';
    //var method = WS_SERVER+'terminal/destinos/idTerminalOrigen/1/format/json';
    console.log("WS: "+method);
    
    $.ajax({
        type: 'get',
        url: method,
        dataType: 'json',
        data: '',
        cache: false,
        success: function(data){
            if (data==null){
                $(form+'_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
            } else {
                var cad = crearDropDownSelectTerminal(data);
                $(form+'_destino').html(cad);
            }
            habilitarElemento(form+'_destino');
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log("ERROR: Ajax Request: "+textStatus+", "+errorThrown);
            console.log("ERROR_METHOD: getTerminalesDestinoAjax");
            console.log("ERROR_WS: "+method);
            
            $(form+'_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
        }
    });
}


function validateUserLogged() {
    if (localStorage.getItem('idUsuario')!=null){
        return true;
    } else {
        return false;
    }
}
/*function getTerminalesOrigenAjax(form){
    var method = base_url+'website/getTerminalesOrigenAjax';

    $.ajax({
        type: 'get',
        url: method,
        dataType: 'json',
        data: '',
        cache: false,
        success: function(data){
            var cad= crearDropDownSelectTerminal(data);
            $(form+'_origen').html(cad);
            $(form+'_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log("ERROR: Ajax Request: "+textStatus+", "+errorThrown);
        }
    });
}*/

/*function getTerminalesDestinoAjax(form){
    deshabilitarElemento(form+'_destino');
    var method = base_url+'website/getTerminalesDestinoAjax';
    var parametros = {
        'idTerminalOrigen': $(form+'_origen option:selected').val()
    };

    $.ajax({
        type: 'get',
        url: method,
        dataType: 'json',
        data: parametros,
        cache: false,
        success: function(data){
            if (data==null){
                $(form+'_destino').html(createSelectOption(0, SELECCIONAR_TEXT, SELECTED_VALUE_SI));
            } else {
                var cad = crearDropDownSelectTerminal(data);
                $(form+'_destino').html(cad);
            }
            habilitarElemento(form+'_destino');
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log("ERROR: Ajax Request: "+textStatus+", "+errorThrown);
        }
    });
}*/